document.addEventListener('DOMContentLoaded', function() {
    const getContentButton = document.getElementById('get-content');
    getContentButton.addEventListener('click', async () => {
        const [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
        chrome.scripting.executeScript({
            target: { tabId: tab.id },
            function: getChatroomContent
        });
    });
});


function getChatroomContent() {
    function run3s() {
        const chatroomItems = document.querySelectorAll('.webcast-chatroom___item.webcast-chatroom___enter-done');
        const contents = [];

        if (chatroomItems.length > 0) {
            chatroomItems.forEach(item => {
                contents.push(item.textContent);
            });

            // 发送 POST 请求
            fetch('http://127.0.0.1:8001/chatroom', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                mode: 'no-cors',
                body: JSON.stringify(contents)
            })
            .then(response => {
                if (!response.ok) {
                    throw new Error(`HTTP error! status: ${response.status}`);
                }
                return response.json();
            })
            .then(data => console.log('Success:', data))
            .catch((error) => console.error('Error:', error));
        } else {
            console.log('未找到 class 为 webcast-chatroom___item webcast-chatroom___enter-done 的元素');
        }
    }

    setInterval(run3s, 8000);
}